﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using IronOne.SalesOne.BusinessObjects;

namespace IronOne.SalesOne.SalesOneWeb.UserManagement
{
    public partial class DisableUserRoles : System.Web.UI.Page
    {

        Facade.StructureFacade structureFacade = new Facade.StructureFacade();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Page.Request.Params["RoleId"] != null)
                {
                   UserRole_aspnet asp_role = structureFacade.GetAspnetUserRole(new Guid(Page.Request.Params["RoleId"]));
                   //txtNewRole.Text = asp_role.RoleName;

                    UserRole role = structureFacade.GetUserRole(asp_role.RoleId);
                    UserType userType = structureFacade.GetUserType(role.UserTypeId);
                    //if (role.Status == 1)
                    //    chkEnabled.Checked = true;
                    SetDataSourceToTypes(structureFacade.GetUserTypes(""));
                    ddlUserType.SelectedValue = role.UserTypeId.ToString();

                    SetDataSourceToRoles(structureFacade.GetUserRoleForUserType(Convert.ToInt32(ddlUserType.SelectedValue)));
                    ddlRoles.SelectedValue = asp_role.RoleId.ToString();
                    //SetDataSourceToTypes(structureFacade.GetUserTypes(""), ddlUserType);
                    //SetDataSourceToTypes(structureFacade.GetUserTypes(""), ddlNewUserType);

                    //ddlNewUserType.SelectedValue = role.UserTypeId.ToString();
                    //ddlUserType.SelectedValue = role.UserTypeId.ToString();
                    //SetDataSourceToRoles(structureFacade.GetUserRoleForUserType(Convert.ToInt32(ddlUserType.SelectedValue)));
                    //ddlRole.SelectedValue = role.RoleId.ToString();
                }
                else
                {
                    SetDataSourceToTypes(structureFacade.GetUserTypes(""));
                }

                //Load All UserTypes To UserTypes
               
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (ddlUserType.SelectedItem.Text.Equals("Select...", StringComparison.InvariantCultureIgnoreCase))
            {
                //Set status to 0
                /*Get Role by name*/
                UserRole_aspnet userRole_aspnet = structureFacade.GetAspnetUserRole(ddlRoles.SelectedItem.Text);

                /*Get ID of the record*/
                Guid roleID = userRole_aspnet.RoleId;

                /*Get Other Records */
                short userTypeID = Convert.ToInt16(ddlUserType.SelectedValue);
                /*Insert To DataBase*/
                UserRole userRole = new UserRole();
                userRole.RoleId = roleID;
                userRole.UserTypeId = userTypeID;
                userRole.Status = 0;
                structureFacade.UpdateUserRole(userRole);
            }
        }

        protected void ddlUserType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlUserType.SelectedIndex > 0)
                //load only filtered roles to ddlroles
                SetDataSourceToRoles(structureFacade.GetUserRoleForUserType(Convert.ToInt32(ddlUserType.SelectedValue)));
        }

        protected void ddlRoles_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        public void SetDataSourceToTypes(List<UserType> userTypes)
        {
            ddlUserType.DataSource = userTypes;
            ddlUserType.DataTextField = "UserTypeName";
            ddlUserType.DataValueField = "UserTypeId";
            ddlUserType.DataBind();

            InsertDDLItems(ddlUserType, "Select...", 0);
            //InsertDDLItems(ddlUserType, "Select All", 1);
        }

        public void SetDataSourceToRoles(List<UserRole_aspnet> userRoles)
        {
            ddlRoles.DataSource = userRoles;
            ddlRoles.DataTextField = "RoleName";
            ddlRoles.DataValueField = "RoleId";
            ddlRoles.DataBind();

            InsertDDLItems(ddlRoles, "Select...", 0);
            //InsertDDLItems(ddlRoles, "Select All", 1);
        }


        public void InsertDDLItems(DropDownList ddl, string Text, int Position)
        {
            //ListItem item = new ListItem(Text,"1");
            //ddl.Items.Insert(Position, item);
            ddl.Items.Insert(Position,Text);
        }
    }
}
